// Promise封装请求   options是一个对象里面的属性有url, data, method, load
//url 对应请求路径  data对象请求的参数  method对应请求方法  load对应是否显示加载中 1显示 0不显示
let util = require('./storage');
let modelStatus = false;
let Sync = my.getSystemInfoSync()

let Device = 'mini_' + Sync.app + '_' + Sync.brand + '_' + Sync.model + '_' + Sync.system + '_' + 'AlipayVersion' + Sync.version
function httpReq(options) {
  let title = '加载中'
  if (options.title) {
    title = options.title
  }
  if (options.load == 1) {
    my.showLoading({
      content: title,
      delay: '1000',
    })
  }
  return new Promise((resolve, reject) => {
    // 所有的请求加上固定的参数  区分是简单请求还是特殊请求
    //如果options.data是对象 表示是简单请求
    if (Object.prototype.toString.call(options.data) === '[Object Object]') {
      options.data.auth_token = '';
      options.data.uuid = '';
    }
    //如果是json字符串的是特殊请求
    if (Object.prototype.toString.call(options.data) === "[object String]") {
      let obj = JSON.parse(options.data)
      obj.auth_token = '';
      obj.uuid = '';
      options.data = JSON.stringify(obj)
    }
    let header;
    let token = util.get('token')
    if (token) {
      header = {
        "content-type": "application/x-www-form-urlencoded",
        "Authorization": token,
        "PlatformIdentification": "alipayApplet",
        "DeviceInfo": Device
      }
    } else {
      header = {
        "content-type": "application/x-www-form-urlencoded",
        "PlatformIdentification": "alipayApplet",
        "DeviceInfo": Device,
      }
    }
    my.request({
      url: options.url,
      data: options.data,
      headers: header,
      method: options.method,
      success: function (res) {
        my.hideLoading();
        if (res.data.StateCode == 0) {
          resolve(res.data); //把请求到的数据发到引用请求的地方
        } else if (res.data.StateCode == 40001) {
          if (options.showModal !== 0) {
            my.alert({
              title: '温馨提示',
              content: res.data.Msg,
              buttonText: '确定',
              success: (Res) => {
                resolve(res.data);
              }
            })
          } else {
            resolve(res.data);
          }
        } else if (res.data.StateCode == 40002) {
          if (options.showModal1 !== 0) {
            my.alert({
              title: '温馨提示',
              content: res.data.Msg,
              buttonText: '确定',
              success: (Res) => {
                util.clear()
                my.reLaunch({
                  url: '/pages/mine/login/login',
                })
              }
            })
          } else {
            resolve(res.data);
          }
        } else {
          resolve(res.data);
        }
      },
      fail: function (err) {
        my.hideLoading();
        reject(err)
      }
    })
  });
}
// 上传图片
function SelectFile(options) {

  return new Promise((resolve, reject) => {
    my.chooseImage({
      count: options.count, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: res => {
        const path = res.apFilePaths[0];
        let token = util.get('token')
        if (options.load == 1) {
          my.showLoading({
            delay: '1000',
            content: '上传中'
          })
        }
        my.uploadFile({
          url: options.url,
          filePath: path,
          fileType: 'image',
          fileName: 'file',
          header: {
            "content-type": "application/x-www-form-urlencoded",
            "Authorization": token,
            "PlatformIdentification": "alipayApplet",
            "DeviceInfo": Device
          }, // 设置请求的 header
          formData: options.data,
          complete: function (gcr) {
            my.hideLoading();
          },
          success: upRes => {
            resolve(upRes); //把请求到的数据发到引用请求的地方
          },
          fail: err => {
            reject(err)
          },
        })
      }
    })
  })
}

module.exports = {
  httpReq,
  SelectFile
}